package com.guoguo.changeskindemo;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.app.AppCompatDelegate;
import androidx.appcompat.app.SkinAppCompatDelegateImpl;
import androidx.drawerlayout.widget.DrawerLayout;

import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;

import com.guoguo.util.SvgaUtils;
import com.opensource.svgaplayer.SVGAImageView;

import java.io.File;

import skin.support.SkinCompatManager;

public class MainActivity extends AppCompatActivity {
    private static String TAG = "MainActivity";
    private static String SKIN_PACKAGE_NAME = "com.ssit.skinplugin01";

    private String mSkinPkgPath = Environment.getExternalStorageDirectory() + File.separator + "night_plugin.apk";
    private String[] mDatas = new String[]{"Activity", "Service", "Activity", "Service", "Activity", "Service", "Activity", "Service"};

    private Button button1, button2, button3, button4;
    private SVGAImageView svgaImage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button1 = findViewById(R.id.button1);
        button2 = findViewById(R.id.button2);
        button3 = findViewById(R.id.button3);
        button4 = findViewById(R.id.button4);
        svgaImage = findViewById(R.id.svgaImage);

        final SvgaUtils svgaUtils = new SvgaUtils(MainActivity.this,svgaImage);
        svgaUtils.initAnimator();
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                svgaUtils.startAnimator("aixin");
            }
        });
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                svgaUtils.startAnimator("bujibao");
            }
        });
        button3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                svgaUtils.startAnimator("shengri");
            }
        });
        button4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                svgaUtils.startAnimator("youleyuan");
            }
        });
    }

    @NonNull
    @Override
    public AppCompatDelegate getDelegate() {
        return SkinAppCompatDelegateImpl.get(this, this);
    }

    public void skinChnage(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.skin_default:
                SkinCompatManager.getInstance().restoreDefaultTheme();
                break;
            case R.id.white:
                //SKIN_LOADER_STRATEGY_BUILD_IN ：后缀加载
                SkinCompatManager.getInstance()
                        .loadSkin(Constants.SKIN_NAME.WHITE, SkinCompatManager.SKIN_LOADER_STRATEGY_BUILD_IN);
                break;
            case R.id.red:
                SkinCompatManager.getInstance()
                        .loadSkin(Constants.SKIN_NAME.RED, SkinCompatManager.SKIN_LOADER_STRATEGY_BUILD_IN);
                break;
        }
    }

//    public void skinChnage(View v) {
//        int id = v.getId();
//        switch (id) {
//            case R.id.red:
//                com.zhy.changeskin.SkinManager.getInstance().changeSkin(mSkinPkgPath, "com.zhy.plugin", new com.zhy.changeskin.callback.ISkinChangingCallback()
//                {
//                    @Override
//                    public void onStart()
//                    {
//                    }
//
//                    @Override
//                    public void onError(Exception e)
//                    {
//                        Toast.makeText(MainActivity.this, "换肤失败", Toast.LENGTH_SHORT).show();
//                    }
//
//                    @Override
//                    public void onComplete()
//                    {
//                        Toast.makeText(MainActivity.this, "换肤成功", Toast.LENGTH_SHORT).show();
//                    }
//                });
//                break;
//            case R.id.white:
//                com.zhy.changeskin.SkinManager.getInstance().removeAnySkin();
//                break;
//            case R.id.green:
//                AssetManager assetManager = null;
//                try
//                {
//                    assetManager = AssetManager.class.newInstance();
//                    Method addAssetPath = assetManager.getClass().getMethod("addAssetPath", String.class);
//                    addAssetPath.invoke(assetManager, mSkinPkgPath);
//
//                    File file = new File(mSkinPkgPath);
//                    Resources superRes = getResources();
//                    Resources mResources = new Resources(assetManager, superRes.getDisplayMetrics(), superRes.getConfiguration());
//
//                    int mainBgId = mResources.getIdentifier("skin_main_bg", "drawable", SKIN_PACKAGE_NAME);
//                    findViewById(R.id.id_bg_main).setBackgroundDrawable(mResources.getDrawable(mainBgId));
//
//
//                } catch (Exception e)
//                {
//                    e.printStackTrace();
//                }
//
//                break;
//        }
//    }

}